<style>
	td {
		text-align: center
	}
</style>
<!-- ///  define Handlebars template  /// -->
<script id="usersListTable" type="text/x-handlebars-template">
	{{#each users}}
	<tr>
		<td><a href="./?wallet={{this.number}}">{{this.number}}</a></td>
		<td data-sort="{{this.wallet.hashrate}}">{{this.readableHashrate}}</td>
		<td data-sort="{{this.wallet.hashes}}">{{this.readableHashes}}</td>
		<td data-sort="{{this.wallet.pending}}">{{this.readablePending}}</td>
		<td data-sort="{{this.wallet.paid}}">{{this.readablePaid}}</td>
		<td data-sort="{{this.wallet.lastShare}}">{{this.timeago}}</td>
	</tr>
	{{/each}}
</script>

<script>
	function parseUsers(wallets) {
		var walletsArray = [],
			properObject = {};
		for(var wallet in wallets) {
			if(wallets.hasOwnProperty(wallet)) {
				var userData = wallets[wallet];
				walletsArray.push({
					number: wallet,
					wallet: userData,
					timeago: $.timeago(new Date(userData.lastShare * 1000).toISOString()),
					readablePending: getReadableCoins(userData.pending),
					readablePaid: getReadableCoins(userData.paid),
					readableHashrate: getReadableHashRateString(userData.hashrate) + '/s',
					readableHashes: getReadableHashRateString(userData.hashes)
				});
			}
		}
		properObject['users'] = walletsArray.sort(function(a, b) {
			return a.wallet.hashrate - b.wallet.hashrate
		}).reverse();

		return properObject;
	}

	function cretaUserTable() {
		$.ajax({
			url: api + '/admin_users',
			data: {password: docCookies.getItem('password')},
			cache: false,
			dataType: 'json',
			success: function(data) {
				renderTemplate(parseUsers(data), '#usersListTable', '#template');
			}
		});
	}

	$(function() {
		$('[data-toggle="tooltip"]').tooltip();
		$('.usersList th.sort').on('click', sortTable);
		cretaUserTable();
	});

</script>
<div class="table-responsive">
	<table class="table table-hover table-striped usersList">
		<thead>
		<tr>
			<th>Wallet</th>
			<th class="sort" style="width:10%;">Hashrate <i class="fa fa-sort"></i></th>
			<th class="sort" style="width:10%;">Hashes <i class="fa fa-sort"></i></th>
			<th class="sort" style="width:16%;">Pending <i class="fa fa-sort"></i></th>
			<th class="sort" style="width:10%;">Paid <i class="fa fa-sort"></i></th>
			<th class="sort" style="width:14%;">Last share <i class="fa fa-sort"></i></th>
		</tr>
		</thead>
		<tbody id="template">

		</tbody>
	</table>
</div>
